<?php

namespace Home\Controller;
use Think\Controller;

class ResumeController extends Controller {

    //简历页面
    public function index(){
        session('entrance', 0);
        $this->assign('curNav', 1);
        if(isset($_SESSION['loginUser'])){
            //获取有无未读消息
            $message_db = D("Message");
            $message_result = $message_db->getIsRead($_SESSION['loginUser']['user_id']);
            if(is_array($message_result)){
                session('is_read',1);
            }else{
                session('is_read',2);
            }
            $this->display();
        }else{
            $this->display('Login/index');
        }
    }
    //PC端简历页面
    public function indexPC(){
        $this->assign('curNav', 1);
        if(isset($_SESSION['loginUser'])){
            $this->theme('PC')->display('Resume/resume');
        }else{
            $this->theme('PC')->display('Login/login');
        }
    }

    //修改个人资料页面
    public function modify(){
        $this->assign('curNav', 3);
        if(isset($_SESSION['loginUser'])){
            $this->display('edit_profile');
        }else{
            $this->display('Login/index');
        }
    }

    //修改加项页面跳转
    public function ext(){
        $this->assign('curNav', 1);
        $resume_id = $_GET['resume_id'];
        if(isset($_SESSION['loginUser'])){
            if (isset($resume_id)) {
                $this->assign('resume_id', $resume_id);
                $this->display('edit_ext');
            } else {
                $this->error('获取数据出错', U('Login/index'));
            }
        }else{
            $this->display('Login/index');
        }
    }

    //PC端修改个人资料,完善资料页面
    public function modifyPC(){
        $this->assign('curNav', 1);
        if(isset($_SESSION['loginUser'])){
            $this->theme('PC')->display('Fulfil/fulfil');
        }else{
            $this->theme('PC')->display('Login/login');
        }
    }


    //简历首页信息
    public function resumeBaseInfo(){
        if(isset($_SESSION['loginUser'])){
            $user_id = $_SESSION['loginUser']['user_id'];
            if(IS_GET){
                $user_db = D('User');
                $user_result = $user_db->getResumeBaseInfo($user_id);
                if(is_array($user_result)){
                    $this->success('获取信息成功',$user_result);
                }else{
                    $this->error('获取信息失败');
                }
            }
        }else{
            $this->error('请登陆系统',U('Login/index'));
        }
    }

    //简历首页信息
    public function resumeBaseInfoPC(){
        if(isset($_SESSION['loginUser'])){
            $user_id = $_SESSION['loginUser']['user_id'];
            if(IS_GET){
                $user_db = D('User');
                $user_result = $user_db->getResumeBaseInfo($user_id);
                if(is_array($user_result)){
                    $this->success('获取信息成功',$user_result);
                }else{
                    $this->error('获取信息失败');
                }
            }
        }else{
            $this->error('请登陆系统',U('Login/indexPC'));
        }
    }

    //PC添加工作经历
    public function addExpPC(){
        if(isset($_SESSION['loginUser'])){
            $user_id = $_SESSION['loginUser']['user_id'];
            if(IS_POST){
                //获取信息

                $data['resume_id'] = $_POST['resume_id'];
                $data['project_name'] = $_POST['project_name'];
                $data['role'] = $_POST['role'];
                $data['description'] = $_POST['address'];
                $data['url'] = $_POST['url'];
                $data['addonurl'] = $_POST['addonurl'];
                $exp_db = D('Experience');

                if($data['addonurl']==null){
                    $data['addonurl']='';
                }
                $data['r_order'] = $exp_db->where("resume_id=%d",$data['resume_id'])->max('r_order')+1;
                $data['update_time'] = date('Y-m-d H:i:s',time());

                $exp_db->add($data);
                $this->success('添加成功');
            }
        }else{
            $this->error('请登陆系统',U('Login/indexPC'));
        }
    }

    //PC修改工作经历
    public function editExpPC(){
        if(isset($_SESSION['loginUser'])){
            $user_id = $_SESSION['loginUser']['user_id'];
            if(IS_POST){
                //获取信息
                $data['exp_id'] = $_POST['exp_id'];
                $data['resume_id'] = $_POST['resume_id'];
                $data['project_name'] = $_POST['project_name'];
                $data['role'] = $_POST['role'];
                $data['description'] = $_POST['address'];
                $data['url'] = $_POST['url'];
                $data['addonurl'] = $_POST['addonurl'];
                $data['update_time'] = date('Y-m-d H:i:s',time());
                $exp_db = D('Experience');
                $exp_db->save($data);
                $this->success('添加成功');
            }
        }else{
            $this->error('请登陆系统',U('Login/indexPC'));
        }
    }
    //完善个人资料页面－获取页面数据
    public function userInfo(){
        if(isset($_SESSION['loginUser'])){
            $user_id = $_SESSION['loginUser']['user_id'];
            if(IS_GET){
                $user_db = D('User');
                $user_result = $user_db->getUserInfo($user_id);
                if(is_array($user_result)){
                    $this->success('获取信息成功',$user_result);
                }else{
                    $this->error('获取信息失败');
                }
            }
        }else{
            $this->error('请登陆系统',U('Login/index'));
        }
    }

    //完善个人资料页面－修改个人资料
    public function userInfoUpdate(){
        if(isset($_SESSION['loginUser'])){
            $user_id = $_SESSION['loginUser']['user_id'];
            if(IS_POST){
                //获取信息
                $user_data['image_url'] = $_POST['image_url'];
                $user_data['user_name'] = $_POST['user_name'];
                $user_data['birthday'] = $_POST['birthday'];
                $user_data['address'] = $_POST['address'];
                $user_data['sex'] = $_POST['sex'];

                $user_ext_data['ispublic_birthday'] = $_POST['ispublic_birthday'];
                $user_ext_data['ispublic_phone'] = $_POST['ispublic_phone'];
                $user_ext_data['ispublic_degree'] = $_POST['ispublic_degree'];
                $user_ext_data['degree_year'] = $_POST['degree_year'];
                $user_ext_data['degree_school'] = $_POST['degree_school'];
                $user_ext_data['degree_major'] = $_POST['degree_major'];
                $user_ext_data['agent_j'] = $_POST['agent_j'];
                $user_ext_data['agent_y'] = $_POST['agent_j'];
                $user_ext_data['agent_j_desc'] = $_POST['agent_j_desc'];
                $user_ext_data['agent_y_desc'] = $_POST['agent_j_desc'];
                $user_ext_data['jobs'] = $_POST['jobs'];
                $user_ext_data['duration'] = $_POST['duration'];
                ini_set('date.timezone','Asia/Shanghai');
                $user_ext_data['update_time'] = date('Y-m-d H:i:s',time());
                $user_db = D('User');
                //dump($user_ext_data);
                $user_result = $user_db->setUserInfo($user_data,$user_ext_data,$user_id);
                if($user_result){
                    $user_db = M('User');
                    $user_info = $user_db->getByUser_id($user_id);
                    session('loginUser',$user_info);
                    //dump($user_result);
                    $this->success('修改成功');
                }else{
                    //dump($user_result);
                    $this->error('修改失败');
                }
            }
        }else{
            $this->error('请登陆系统',U('Login/index'));
        }
    }


    //简历首页－是否公开简历
    public function isPublicResume(){
        if(IS_POST){
            $user_id = $_SESSION['loginUser']['user_id'];
            $data['is_public'] = $_POST['is_public'];
            ini_set('date.timezone','Asia/Shanghai');
            $data['update_time'] = date('Y-m-d H:i:s',time());
            $resume_db = M('Resume');
            $list = $resume_db->where(array('user_id'=>$user_id))->select();
            if(sizeof($list)==0){
                $data['user_id'] = $user_id;
                $resume_result = $resume_db->add($data);
            }else{
                $resume_result = $resume_db->where(array('user_id'=>$user_id))->save($data);
            }
            if($resume_result){
                $this->success('修改成功');
            }else{
                $this->error(' 修改失败');
            }
        }
    }

    //简历首页- 简历描述
    public function resumeDescription(){
        if(IS_POST){
            $user_id = $_SESSION['loginUser']['user_id'];
            $data['introduction'] = $_POST['introduction'];
            ini_set('date.timezone','Asia/Shanghai');
            $data['update_time'] = date('Y-m-d H:i:s',time());
            $resume_db = M('Resume');
            $resume_result = $resume_db->where(array('user_id'=>$user_id))->save($data);
            if($resume_result){
                $this->success('修改成功');
            }else{
                $this->error(' 修改失败');
            }
        }
    }


    //简历首页－添加工作经历
    /*
     * 首次创建时，前台提交的exp_id为空；当经历被创建后，提交的exp_id为该项目经历的exp_id
     */
    public function addExperience(){
        if(IS_POST){
            $data['resume_id'] = $_POST['resume_id'];
            $data['project_name'] = $_POST['project_name'];
            $data['role'] = $_POST['role'];
            ini_set('date.timezone','Asia/Shanghai');
            $data['update_time'] = date('Y-m-d H:i:s',time());

            //dump($data);
            $experience_db = D('Experience');

            $exp_id = $_GET['exp_id'];
            if($exp_id){
                $experience_result = $experience_db->where(array('exp_id'=>$exp_id))->save($data);
            }else{
                $data['r_order'] = $experience_db->where("resume_id=%d",$data['resume_id'])->max('r_order')+1;
                $experience_result = $experience_db->add($data);
            }

            if($experience_result){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
    }



    //简历附件-index
    public function editExperience(){
        $this->assign('curNav', 1);
        if(isset($_SESSION['loginUser'])){
            $exp_id = $_GET['exp_id'];
            $experience_db = D('Experience');
            $experience_result = $experience_db->getExperienceInfo($exp_id);
            $this->assign('data',$experience_result);
            $this->display('edit_exp');
        }else{
            $this->error('请登录系统',U('Login/index'));
        }
    }


    //简历附件－附件ajax
    public function getAtt(){
        if(IS_GET){
            $exp_id = $_GET['exp_id'];
            $attachment_db = M('Attachment');
            $attachment_result = $attachment_db->where(array('exp_id'=>$exp_id))->select();
            //dump($attachment_result);
            if(is_array($attachment_result)){
                $this->success('获取成功',$attachment_result);
            }else{
                $this->error('获取失败');
            }
        }else{
            $this->error('错误方法');
        }
    }

    //简历附件－详细描述
    public function descriptionInfo(){
        if(IS_POST){
            $data['description'] = $_POST['description'];
            $exp_id = $_POST['exp_id'];
            ini_set('date.timezone','Asia/Shanghai');
            $data['update_time'] = date('Y-m-d H:i:s',time());
            //dump($data);
            $experience_db = M('Experience');
            $experience_result = $experience_db->where(array('exp_id'=>$exp_id))->save($data);
            if($experience_result){
                $this->success('修改成功');;
            }else{
                $this->error('修改失败');
            }
        }

    }


    //简历附件－作品链接
    public function urlInfo(){
        if(IS_POST){
            $url = $_POST['url'];
            $url_arr = explode('http://',$url);
            if( NULL == $url_arr[0]){
                $data['url'] = $url_arr[1];
            }else{
                $data['url'] = $url_arr[0];
            }
            //$data['url'] = $_POST['url'];
            $exp_id = $_POST['exp_id'];
            ini_set('date.timezone','Asia/Shanghai');
            $data['update_time'] = date('Y-m-d H:i:s',time());
            $experience_db = M('Experience');
            $experience_result = $experience_db->where(array('exp_id'=>$exp_id))->save($data);
            //dump($experience_result);
            if($experience_result){
                $this->success('修改成功');;
            }else{
                $this->error('修改失败');
            }
        }
    }


    //test
    public function test(){
        $user_id = $_GET['user_id'];
        $user_db = D('User');
        $user_result = $user_db->getResumeBaseInfo($user_id);
        dump($user_result);

//        $data['resume_id'] = $_GET['resume_id'];
//        $data['project_name'] = $_GET['project_name'];
//        $data['role'] = $_GET['role'];
//        ini_set('date.timezone','Asia/Shanghai');
//        $data['update_time'] = date('Y-m-d H:i:s',time());
//        dump($data);
//        $experience_db = D('Experience');
//        $exp_id = $_GET['exp_id'];
//        if($exp_id){
//            $experience_result = $experience_db->where(array('exp_id'=>$exp_id))->save($data);
//        }else{
//            $experience_result = $experience_db->add($data);
//        }
//        dump($experience_result);

//        $exp_id = $_GET['exp_id'];
//        dump($exp_id);
//        $experience_db = D('Experience');
//        $experience_result = $experience_db->getExperienceInfo($exp_id);
//        dump($experience_result);
    }

      public function exp_order($exp_id=0,$old_order,$new_order){
          if(isset($_SESSION['loginUser'])){
              $user_id = $_SESSION['loginUser']['user_id'];
              if($exp_id==0){
                  $this->error('获取信息失败');
              }else{
                  $resume_db = D('Resume');
                  $resume_ids = $resume_db->where("user_id=%d",$user_id)->select();
                  if(sizeof($resume_ids)==1){
                      if($old_order == $new_order){
                          $this->error('获取信息失败');
                      }
                      $exp_db= D('Experience');
                      $exp_list = $exp_db->where("resume_id=%d",$resume_ids[0]['resume_id'])->order('r_order asc')->select();

                      for($i=0;$i<sizeof($exp_list);$i++){
                          if($old_order > $new_order){
                              if($exp_list[$i]['r_order'] == $old_order){
                                  $exp_list[$i]['r_order'] = $new_order;
                                  $exp_db->save($exp_list[$i]);
                              }elseif($exp_list[$i]['r_order']<$old_order && $exp_list[$i]['r_order']>=$new_order){
                                  $exp_list[$i]['r_order'] = $exp_list[$i]['r_order']+1;
                                  $exp_db->save($exp_list[$i]);
                              }
                          }else{
                              if($exp_list[$i]['r_order'] == $old_order){
                                  $exp_list[$i]['r_order'] = $new_order;
                                  $exp_db->save($exp_list[$i]);
                              }elseif($exp_list[$i]['r_order']<=$new_order && $exp_list[$i]['r_order']>$old_order){
                                  $exp_list[$i]['r_order'] = $exp_list[$i]['r_order']-1;
                                  $exp_db->save($exp_list[$i]);
                              }
                          }
                      }

                      $list = $exp_db->field(array('exp_id','project_name','role','r_order'))->where("resume_id=%d",$resume_ids[0]['resume_id'])->order('r_order asc')->select();
                      //dump($list);
                      $this->success('获取信息成功',$list);
                  }else{
                      $this->error('获取信息失败');
                  }

              }

          }else{
              $this->error('请登陆系统',U('Login/index'));
          }
      }

    //修改项目名称
    public function changeProjectName(){
        if(IS_POST){
            $exp_id = $_POST['exp_id'];
            $data['project_name'] = $_POST['project_name'];
            $experience_db = M('Experience');
            $experience_result = $experience_db->where(array('exp_id'=>$exp_id))->save($data);
            if($experience_result){
               $this->success('修改成功');
            }else if( 0 == $experience_result ){
                $this->success('项目名称未变');
            }else{
                $this->error('项目名称修改失败');
            }

        }else{
            $this->error('错误方法');
        }
    }

    //工作岗位修改
    public function changeJobsName(){
        if(IS_POST){
            $exp_id = $_POST['exp_id'];
            $data['role'] = $_POST['role'];
            $exprience_db = M('Experience');
            $exprience_result = $exprience_db->where(array('exp_id'=>$exp_id))->save($data);
            if($exprience_result){
                $this->success('修改成功');
            }else if( 0 == $exprience_result){
                $this->success('工作岗位未变');
            }else{
                $this->error('工作岗位修改失败');
            }
        }else{
            $this->error('错误方法');
        }
    }

    public function editResumePC(){
        if(isset($_SESSION['loginUser'])){
            $user_id = $_SESSION['loginUser']['user_id'];

            $resume_id = $_POST['resume_id'];
            $is_public = $_POST['is_public'];
            $introduction = $_POST['introduction'];
            $imageurls = $_POST['imageurls'];
            $videourl = $_POST['videourl'];

            $data['resume_id'] = $resume_id;
            $data['is_public'] = $is_public;
            $data['introduction'] = $introduction;
            $resume_db = D('Resume');
            $resume_db->save($data);

            $data_att['resume_id'] = $resume_id;
            $data_att['image_url1'] = $imageurls;
            $data_att['video_url1'] = $videourl;

            $resume_ext_db = M('resume_att');
            $result = $resume_ext_db->where("resume_id=%d",$resume_id)->select();
            if(sizeof($result)==1){
                $resume_ext_db->where("resume_att_id=%d",$result[0]['resume_att_id'])->save($data_att);
            }else{
                $resume_ext_db->add($data_att);
            }
            $this->success('修改成功');
        }else{
            $this->error('请登陆系统',U('Login/indexPC'));
        }
    }

    public function exp_orderPC($exp_id=0,$old_order,$new_order){
        if(isset($_SESSION['loginUser'])){
            $user_id = $_SESSION['loginUser']['user_id'];
            if($exp_id==0){
                $this->error('获取信息失败');
            }else{
                $resume_db = D('Resume');
                $resume_ids = $resume_db->where("user_id=%d",$user_id)->select();
                if(sizeof($resume_ids)==1){
                    if($old_order == $new_order){
                        $this->error('获取信息失败');
                    }
                    $exp_db= D('Experience');
                    $exp_list = $exp_db->where("resume_id=%d",$resume_ids[0]['resume_id'])->order('r_order asc')->select();

                    for($i=0;$i<sizeof($exp_list);$i++){
                        if($old_order > $new_order){
                            if($exp_list[$i]['r_order'] == $old_order){
                                $exp_list[$i]['r_order'] = $new_order;
                                $exp_db->save($exp_list[$i]);
                            }elseif($exp_list[$i]['r_order']<$old_order && $exp_list[$i]['r_order']>=$new_order){
                                $exp_list[$i]['r_order'] = $exp_list[$i]['r_order']+1;
                                $exp_db->save($exp_list[$i]);
                            }
                        }else{
                            if($exp_list[$i]['r_order'] == $old_order){
                                $exp_list[$i]['r_order'] = $new_order;
                                $exp_db->save($exp_list[$i]);
                            }elseif($exp_list[$i]['r_order']<=$new_order && $exp_list[$i]['r_order']>$old_order){
                                $exp_list[$i]['r_order'] = $exp_list[$i]['r_order']-1;
                                $exp_db->save($exp_list[$i]);
                            }
                        }
                    }

                    $list = $exp_db->where("resume_id=%d",$resume_ids[0]['resume_id'])->order('r_order asc')->select();
                    //dump($list);
                    $this->success('获取信息成功',$list);
                }else{
                    $this->error('获取信息失败');
                }

            }

        }else{
            $this->error('请登陆系统',U('Login/index'));
        }
    }

    //获取加项信息
    public function getAttachmentInfo(){
        if(IS_GET){
            $resume_id = $_GET['resume_id'];
            $resume_att_db = M('Resume_att');
            $resume_att_result = $resume_att_db->getByResume_id($resume_id);
            //dump($resume_att_result);
            if(is_array($resume_att_result)){
                $resume_att_return['resume_att_id'] = $resume_att_result['resume_att_id'];
                $resume_att_return['resume_id'] = $resume_att_result['resume_id'];
                $signal = 0;
                for( $i = 1 ; $i < 9 ; $i++ ){
                    if( $resume_att_result['image_url'.$i] != NULL ){
                        $resume_att_return['image'][$signal] = array( 'image_url'.$i => $resume_att_result['image_url'.$i]);
                        $signal += 1;
                    }
                }
                $resume_att_return['video_url1'] = $resume_att_result['video_url1'];
//                if( $resume_att_result['video_url1'] != NULL ){
//                        $resume_att_return['video'] = array('video_url1'=>$resume_att_result['video_url1']);
//                }
                //dump($resume_att_return);
                $this->success('获取信息成功',$resume_att_return);
            }else{
                $this->error('获取信息失败');
            }
        }else{
            $this->error('错误方法');
        }
    }


}